package com.example.second.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter {
    private static final String DATABASE_NAME = "MemberList.db";
    private static final String DATABASE_TABLE = "MemberList";
    private static final int DATABASE_VERSION = 1;

    private static final String KEY_ID = "_id";
    private static final String KEY_NAME = "name";
    private static final String KEY_MOBILE = "mobile";
    private static final String KEY_IDENTITY = "identity";
    private static final String KEY_PASSWORD = "password";

    private static final int TASK_COLUMN = 4;

    private final Context context;
    private SQLiteDatabase db;
    private DbOpenHelper dbHelper;

    public DbAdapter(Context context) {
        this.context = context;
        dbHelper = new DbOpenHelper(this.context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void open() {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    public long insertTask(String _task) {
        ContentValues newTaskValues = new ContentValues();

        newTaskValues.put(KEY_NAME, _task);

        return db.insert(DATABASE_TABLE, null, newTaskValues);
    }

    public boolean removeTask(long _rowIndex) {
        return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
    }

    public boolean updateTask(long _rowIndex, String _task) {
        ContentValues newValue = new ContentValues();
        newValue.put(KEY_TASK, _task);
        return db.update(DATABASE_TABLE, newValue, KEY_ID + "=" + _rowIndex, null) > 0;
    }

    public Cursor getAllToDoItemsCursor() {
        return db.query(DATABASE_TABLE, new String[] { KEY_ID, KEY_TASK },
                null, null, null, null, null);
    }

    public Cursor setCursorToToDoItem(long _rowIndex) throws SQLException {
        Cursor result = db.query(true, DATABASE_TABLE, new String[] { KEY_ID,
                KEY_TASK }, KEY_ID + "=" + _rowIndex, null, null, null, null,
                null);

        if ((result.getCount() == 0) || !result.moveToFirst()) {
            throw new SQLException("No Todo items found for row: " + _rowIndex);
        }
        return result;
    }

    public String getToDoItem(long _rowIndex) throws SQLException {
        Cursor cursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ID,
                KEY_TASK }, KEY_ID + "=" + _rowIndex, null, null, null, null,
                null);
        if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
            throw new SQLException("No to do item found for row: " + _rowIndex);
        }

        String task = cursor.getString(TASK_COLUMN);
        return task;
    }

    private static class DbOpenHelper extends SQLiteOpenHelper {
        private static final String DATABASE_CREATE = String.format(
                "create table %s (%s integer primary key autoincrement, "
                + "%s text not null, "
                + "%s text not null, "
                + "%s text not null, "
                + "%s text not null);"
                , DATABASE_TABLE, KEY_ID, KEY_NAME, KEY_MOBILE, KEY_IDENTITY, KEY_PASSWORD);

        public DbOpenHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
                int _newVersion) {
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(_db);
        }
    }
}
